package xdu.lz.stage2_design_pattern.chapter15_thread_per_message_pattern;

import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MessageHandler {

    private static final Random random = new Random(System.currentTimeMillis());

    private static final Executor executor = Executors.newFixedThreadPool(5);
    public void request(Message message){

        executor.execute(()->{
            String value = message.getMessage();
            try {
                System.out.println(value+" is handle by "+Thread.currentThread().getName());
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
       /* new Thread(()->{
             String value = message.getMessage();
            try {
                System.out.println(value+" is handle by "+Thread.currentThread().getName());
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();*/
    }

    public void shutdown(){
        ((ExecutorService)executor).shutdown();
    }
}
